import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../lib_user/user_manager.dart';
import '../root_page.dart';
// import 'package:lib_basemodel/lib_basemodel.dart';
// import 'package:lib_baseview/lib_baseview.dart';
// import 'package:lib_user/lib_user.dart';
// import 'package:m_chatroom/m_chatroom.dart';

class JrNavigator {
  // yqdf://target=xxx&x=y&...
  //
  // target: 跳转目标
  // workbench：嘉宾工作台
  // room：房间
  static const _BASE_NAVIGATOR_URL = "yqdf://target=";
  static const NAVIGATOR_URL_WORKBENCH = "${_BASE_NAVIGATOR_URL}workbench";
  static const NAVIGATOR_URL_ROOM = "${_BASE_NAVIGATOR_URL}room";

  //初始化通信管道-设置退出到手机桌面
  static const String CHANNEL = "android/back/desktop";
  //设置回退到手机桌面
  static Future<bool> backDeskTop() async {
    final platform = MethodChannel(CHANNEL);
    //通知安卓返回,到手机桌面
    try {
      final bool out = await platform.invokeMethod('backDesktop');
      if (out) debugPrint('返回到桌面');
    } on PlatformException catch (e) {
      debugPrint("通信失败(设置回退到安卓手机桌面:设置失败)");
      print(e.toString());
    }
    return Future.value(false);
  }

  // static navigateToWorkBench() {
  //   if (UserManager.instance.currentUser?.gsWorkbenchUrl.isNotEmpty == true) {
  //     CommonWebView.show(rootNavigatorState.currentContext!,
  //         UserManager.instance.currentUser?.gsWorkbenchUrl ?? '',
  //         title: T.current!.guest_workbench);
  //   } else {
  //     Navigator.pushNamed(
  //       rootNavigatorState.currentContext!,
  //       NAVIGATOR_URL_WORKBENCH,
  //     );
  //   }
  // }

  // static navigateToRoom() {
  //   // Navigator.pop(rootNavigatorState.currentContext!);
  //   jumpToHallPage(
  //     rootNavigatorState.currentContext!,
  //     $fixnum.Int64(14457212744432640), //测试
  //     false,
  //     false,
  //   );
  // }

  static jumpAndRemoveAll(Widget widget,
      {BuildContext? context, bool withoutAnimation = false}) async {
    if (withoutAnimation) {
      await Navigator.of(context ?? rootNavigatorState.currentContext!)
          .pushAndRemoveUntil(
              PageRouteBuilder(
                pageBuilder: (_, __, ___) => widget,
                transitionDuration: Duration(seconds: 0),
              ),
              (Route<dynamic> route) => false);
      return;
    }
    await Navigator.of(context ?? rootNavigatorState.currentContext!)
        .pushAndRemoveUntil(MaterialPageRoute(builder: (context) {
      return widget;
    }), (Route<dynamic> route) => false);
  }

  // static jumpToPageByUrl(String jumpUrl) {
  //   if (jumpUrl.contains("yqdf://yqdf.xyz/room")) {
  //     _analysisUrl(jumpUrl);
  //   } else if (jumpUrl.contains("yqdf://yqdf.xyz/workbench")) {
  //     navigateToWorkBench();
  //   } else {
  //     SchemeHelper.checkAndGo(Get.context!, jumpUrl);
  //   }
  // }

  static _analysisUrl(String jumpUrl) {
    if (!jumpUrl.contains("?")) {}
    var array1 = jumpUrl.split("?");
    String string1 = array1.first;
    String string2 = array1.last;

    String roomId = "";
    var arrtemp = string2.split("=");
    roomId = arrtemp.last;
    print("解析完成");

    // if (roomId != "") {
    //   RoomPage.show(
    //     rootNavigatorState.currentContext!,
    //     int.parse(roomId),
    //   );
    // }
  }
}
